//
//  BKIntegralListViewController.m
//  bestkeep
//
//  Created by yons on 16/12/5.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKIntegralListViewController.h"
#import "BKIntegralListViewCell.h"
#import "BKIntegralDetailHeaderView.h"
#import "BKMainCommon.h"
#import "BKIntegralListModel.h"

@interface BKIntegralListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *marr_data;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)BKIntegralDetailHeaderView *view_BKIntegralDetailHeader;
@property(nonatomic,strong)NSString *page;
@property(nonatomic,assign)BOOL isFirst;
@property(nonatomic,strong)ExceptionView *exceptionView;

@end

@implementation BKIntegralListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setProperty];
    [self createUI];
    [self setUpRefresh];
    [self check_NetWork];
    
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

#pragma mark==================setProperty======================
-(void)setProperty{
    
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        self.navigationController.navigationBar.clipsToBounds = NO;
    }
    
    self.view.backgroundColor = RGB(64, 64, 64);
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.title = @"积分明细";
    self.page = @"1";
    self.isFirst = YES;
    
}


#pragma mark==================checkNet======================
-(void)check_NetWork{
    RequestReachabilityStatus status = [RequestManager reachabilityStatus];
    switch (status) {
        case RequestReachabilityStatusReachableViaWiFi:
        case RequestReachabilityStatusReachableViaWWAN: {
            [self BK_getData_IntegralListre];
            
        }
            break;
            
        default: {
            [HUD dismiss];
            
            self.exceptionView = [ExceptionView exceptionViewWithType:ExceptionCodeTypeNoNetwork];
            ExceptionViewAction *action = [ExceptionViewAction actionWithType:ExceptionCodeTypeNoNetwork handler:^(ExceptionViewAction *action) {
                [self.exceptionView dismiss];
                self.exceptionView = nil;
                [self check_NetWork];
            }];
            [self.exceptionView addAction:action];
            [self.exceptionView showInView:self.view];
            
        }
            break;
    }
    
}


-(void)setUpRefresh{
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMoreDate)];
    footer.stateLabel.textColor = [UIColor lightGrayColor];
    footer.stateLabel.font = [UIFont systemFontOfSize:14];
    footer.arrowView.image = [UIImage imageNamed:@"BKGDPullDown"];
    UIActivityIndicatorView *loadingView = [footer valueForKeyPath:@"loadingView"];
    loadingView.hidesWhenStopped = YES;
    loadingView.color=RGB(42, 187, 155);
    [footer addSubview:loadingView];
    
    _tableView.mj_footer = footer;
    _tableView.mj_footer.automaticallyHidden = YES;
    
}


- (UIView *)getViewWhenDataEndLoading {
    
    UIView *noMoreView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, ScreenWidth, self.view.frame.size.height-150)];
    noMoreView.backgroundColor = RGBACOLOR(242, 242, 242, 1);
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120*0.66, 116*0.66)];
    UILabel *promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    [noMoreView addSubview:promptLabel];
    promptLabel.text = @"暂无明细记录";
    [promptLabel setTextAlignment:NSTextAlignmentCenter];
    promptLabel.font = [UIFont systemFontOfSize:15];
    promptLabel.backgroundColor = [UIColor clearColor];
    promptLabel.textColor = RGBACOLOR(136, 136, 136, 1);
    [noMoreView addSubview:imageView];
    
    CGPoint center = noMoreView.center;
    imageView.center = CGPointMake(center.x, center.y-150-25);
    promptLabel.center = CGPointMake(center.x, CGRectGetMaxY(imageView.frame)+20);
    imageView.image = [UIImage imageNamed:@"BKPhone_kongicoaAn"];
    return noMoreView;
    
}

-(void)reloadMoreDate{
    self.isFirst = NO;
    _page = [NSString stringWithFormat:@"%d",[_page intValue]+1];
    [self BK_getData_IntegralListre];
}


-(void)BK_getData_IntegralListre{
//    if (self.isFirst == YES) {
//        [LoadingView loadingShoppingCartInView:self.view];
//    }
    
    [BKIntegralCommon getUserIntegralRecord:self.page callback:^(BOOL isSucess, id obj, NSError *error) {
        if (isSucess) {
            NSLog(@"%@",obj);
            NSArray *itemArr = obj[@"list"];
            if (itemArr.count == 0&&self.marr_data.count==0) {
                [self.view addSubview:[self getViewWhenDataEndLoading]];
                _tableView.mj_footer = nil;
                _view_BKIntegralDetailHeader.numberLabel.text = [NSString stringWithFormat:@"%@",obj[@"integralAmount"]];
            }else{
                
                if (itemArr.count%20 != 0) {
                    _tableView.mj_footer = nil;
                }
                for (NSDictionary *itemDic in itemArr) {
                    [self.marr_data addObject:itemDic];
                }
                
                _view_BKIntegralDetailHeader.numberLabel.text = [NSString stringWithFormat:@"%@",obj[@"integralAmount"]];
                
                [self.tableView reloadData];
                
            }
            
            
        }
        [_tableView.mj_footer endRefreshing];
        [LoadingView dismiss];
    }];
}


-(void)createUI{
    
    _view_BKIntegralDetailHeader = [[BKIntegralDetailHeaderView alloc]init];
    _view_BKIntegralDetailHeader.frame = CGRectMake(0, 0, ScreenWidth, 150);
    [self.view addSubview:_view_BKIntegralDetailHeader];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = RGB(242, 242, 242);
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[BKIntegralListViewCell class] forCellReuseIdentifier:@"cell"];
    _tableView.tableFooterView = [[UIView alloc]init];
    
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(_view_BKIntegralDetailHeader.mas_bottom);
        make.bottom.mas_equalTo(self.view.mas_bottom);
        
    }];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.marr_data.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BKIntegralListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = [self.marr_data objectAtIndex:indexPath.row];
    BKIntegralListModel *model = [BKIntegralListModel getModelWithDic:dic];
    cell.lab_name.text = model.title                                                           ;
    cell.lab_date.text = model.createdDateStr;
    if ([model.changeType isEqualToString:@"1"]) {
        cell.lab_money.textColor = RGB(0, 186, 156);
        if ([model.integralAmount rangeOfString:@"+" ].location!=NSNotFound) {
            cell.lab_money.text =  [NSString stringWithFormat:@"%@",model.integralAmount];
        }else{
            cell.lab_money.text =  [NSString stringWithFormat:@"+%@",model.integralAmount];
        }
    }else{
        cell.lab_money.textColor = RGB(193, 62, 62);
        if ([model.integralAmount rangeOfString:@"-" ].location!=NSNotFound) {
            cell.lab_money.text =  [NSString stringWithFormat:@"%@",model.integralAmount];
        }else{
            cell.lab_money.text =  [NSString stringWithFormat:@"-%@",model.integralAmount];
        }
    }
    
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 0.0001f;
}

-(NSMutableArray *)marr_data{
    if (!_marr_data) {
        _marr_data = [[NSMutableArray alloc]init];
    }
    return _marr_data;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end





























